package servlet;

import bean.Admin;
import service.AdminServcie;
import sun.security.pkcs11.wrapper.CK_X9_42_DH1_DERIVE_PARAMS;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/login")
public class AdmiServlet extends HttpServlet {
  private AdminServcie as=  new AdminServcie();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String uname = req.getParameter("uname");
        String pwd = req.getParameter("pwd");
        Admin admin = as.login(uname, pwd);
        PrintWriter out = resp.getWriter();
        if (admin!=null){
            Cookie ck1 = new Cookie("uname", uname);
            Cookie ck2 = new Cookie("pwd", pwd);
            ck1.setMaxAge(60*60);
            ck2.setMaxAge(60*60);
            resp.addCookie(ck1);
            resp.addCookie(ck2);
            HttpSession session = req.getSession();
           session.setAttribute("admin",admin );
            resp.sendRedirect("student?method=findAllStudent");
        }else {
            out.print("<h2 style='color:red'>登录失败</h2>");
        }

    }
}
